﻿#pragma execution_character_set("utf-8")
#include "loginwidget.h"
#include "ui_loginwidget.h"
#include <QImage>
#include <QPixmap>
#include <QPainter>
#include "database.h"
#include "mainwidget.h"

LoginWidget::LoginWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginWidget)
{
    ui->setupUi(this);
}

LoginWidget::~LoginWidget()
{
    delete ui;
}

void LoginWidget::showEvent(QShowEvent *event)
{

}

void LoginWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QImage t_image;
    //    t_image.load(":/image/bg.png");
    //    t_image = t_image.scaled(this->width(),this->height());
    //    painter.drawPixmap(0,0,this->width(),this->height(),QPixmap::fromImage(t_image));

}

void LoginWidget::resizeEvent(QResizeEvent *event)
{

}

void LoginWidget::on_pushButton_1_clicked()
{
    loginManage();
}

void LoginWidget::loginManage()
{
    //界面数据
    QString str1 = ui->lineEdit_1->text();
    QString str2 = ui->lineEdit_2->text();
     MainWidget::currentphone = str1;
     MainWidget::currentpasswd = str2;


    if(str1.length() < 2 || str2.length() < 2){
        QMessageBox msgBox;
        msgBox.setWindowTitle("提示");
        msgBox.setText("账号或密码不能为空");
        msgBox.exec();
        return;
    }
    //打开数据库
    if (! DataBasePri::database.open()){
        qCritical() << DataBasePri::database.lastError().text();
        return;
    }
    //数操作据对象
    QSqlQuery query(DataBasePri::database);
    //操作内容
    QString instr = QString::fromLocal8Bit
            ("select * from user where phone = '%1'")
            .arg(str1);
    //这里执行数据库操作
    bool ret = query.exec(instr);
    qDebug()<<"ddddddddd"<<query.lastError();
    //关闭数据库
    DataBasePri::database.close();

    //定义变量
    QString t_password;
    QString t_name;
    while (query.next()){
        t_name = query.value(1).toString();
        t_password = query.value(2).toString();
        MainWidget::currentUser =  query.value(5).toString();
    }
    //判断密码和数据库中的密码是否一致
    if(str2 == t_password){
        //如果一致则进入软件
        if(MainWidget::currentUser == "true"){
            emit sendDisWidget("m_AdminWidget");
        }else{
            emit sendDisWidget("m_SelectWidget");
        }

    }else{
        QMessageBox msgBox;
        msgBox.setWindowTitle("提示");
        msgBox.setText("账号密码不匹配！请重新输入！");
        msgBox.exec();
    }

}

void LoginWidget::loginSupplier()
{

}

void LoginWidget::on_pushButton_2_clicked()
{
    emit sendDisWidget("m_ResigerWidget");
}
